package com.yanqu.road.server.cmd.activity.huashigang;

import com.yanqu.road.entity.activity.huashigang.HuaShiGangActivityEventConfig;
import com.yanqu.road.entity.activity.seasontask.SeasonTaskConditionData;
import com.yanqu.road.entity.config.goods.OpenGoodsBagResult;
import com.yanqu.road.entity.enums.*;
import com.yanqu.road.entity.enums.activity.huashigang.eHuaShiGangEventType;
import com.yanqu.road.entity.enums.activity.huashigang.eHuaShiGangMessageType;
import com.yanqu.road.entity.log.LogHuaShiGangAttackLog;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.huashigang.HuaShiGangProto;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.gameplayer.module.vehicle.VehicleModule;
import com.yanqu.road.server.manger.activity.huashigang.HuaShiGangMgr;
import com.yanqu.road.server.manger.config.GoodsBagMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.player.WorldMessageMgr;
import com.yanqu.road.server.pb.WorldMessagePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.List;

/**
 * @Author wpj
 * @Date 2020/10/7
 */
@Cmd(code = GameProtocol.S_HUA_SHI_GANG_ATTACK_MONSTER_FROM_CROSS, desc = "花石纲攻击怪物")
public class AttackMonsterFromCrossCmd extends AbstractCommand {

    int costNum;
    boolean isKill;
    int eventId;
    long damage;
    long addScore;
    long newBlood;
    UserBaseInfo userBaseInfo;
    int activityId;
    int talentId;
    long allScore;
    int monsterId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        HuaShiGangProto.AttackMonsterFromCross msg = HuaShiGangProto.AttackMonsterFromCross.parseFrom(packet.getBytes());
        ret = msg.getRet();
        costNum = msg.getCostNum();
        eventId = msg.getEventId();
        damage = msg.getDamage();
        isKill = msg.getIsKill();
        addScore = msg.getAddScore();
        newBlood = msg.getNewBlood();
        userBaseInfo = PlayerBasePb.parseToUserBaseInfo(msg.getPlayerInfo());
        activityId = msg.getActivityId();
        talentId = msg.getTalentId();
        allScore = msg.getAllScore();
        monsterId = msg.getMonsterId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        // 出战令恢复
        player.getModule(PreReduceModule.class).restorePreReduce(HuaShiGangMgr.getHuaShiGangTokenId() + "=" + costNum);
        Property killProperty = new Property();
        String reward = "";
        if (ret == 0) {
            CurrencyModule currencyModule = player.getModule(CurrencyModule.class);
            // 消耗出战令
            currencyModule.removeCurrency(PropertyHelper.parseStringToProperty(HuaShiGangMgr.getHuaShiGangTokenId() + "=" + costNum), eLogMoneyType.HuaShiGang, eLogMoneyType.HuaShiGangAttackMonster);

            HuaShiGangActivityEventConfig config = HuaShiGangMgr.getHuaShiGangActivityEventConfigMap().get(eventId);
            if (isKill) {

                List<String> rewardList = StringUtils.stringToStringList(config.getRewards(), "\\|");

                // 击杀奖励（随机奖励+固定奖励，积分在跨服加过了）

                OpenGoodsBagResult result = GoodsBagMgr.getRandomGoods(Integer.valueOf(rewardList.get(0)));
                killProperty.addProperty(result.getReward());

                if (rewardList.size() > 1) {
                    killProperty.addProperty(PropertyHelper.parseStringToProperty(rewardList.get(1)));
                }

                // 添加奖励
                currencyModule.addCurrency(killProperty, eLogMoneyType.HuaShiGang, eLogMoneyType.HuaShiGangKillMonster);

                // 统计上公告奖励
                Property noticeReward = new Property();
                for (Integer itemId : HuaShiGangMgr.getNewConfig().getBossNoticeItemId()) {
                    BigInteger count = killProperty.getCountByGoodsId(itemId);
                    if (count.intValue() > 0) {
                        noticeReward.addProperty(itemId, count);
                    }
                }

                if (result.getNotice() == 2 || !noticeReward.isNothing()) {
                    String killRandomNoticeReward = PropertyHelper.parsePropertyToString(result.getReward());
                    StringBuilder rewardBuilder = new StringBuilder();
                    if (config.getEventType() == eHuaShiGangEventType.NORMAL_MONSTER.getValue()) {
                        rewardBuilder.append(eHuaShiGangMessageType.NORMAL_MONSTER_KILL.getValue());
                        rewardBuilder.append(GameConfig.TALK_SEPARATOR);
                        rewardBuilder.append(config.getEventId());
                        rewardBuilder.append(GameConfig.TALK_SEPARATOR);
                        rewardBuilder.append(killRandomNoticeReward);
                    } else if (config.getEventType() == eHuaShiGangEventType.CREAM_MONSTER.getValue()) {
                        rewardBuilder.append(eHuaShiGangMessageType.CREAM_MONSTER_KILL.getValue());
                        rewardBuilder.append(GameConfig.TALK_SEPARATOR);
                        rewardBuilder.append(talentId);//派遣的门客
                        rewardBuilder.append(GameConfig.TALK_SEPARATOR);
                        rewardBuilder.append(config.getEventId());
                        rewardBuilder.append(GameConfig.TALK_SEPARATOR);
                        rewardBuilder.append(killRandomNoticeReward);
                    } else if (config.getEventType() == eHuaShiGangEventType.BOSS_MONSTER.getValue()) {
                        if (!noticeReward.isNothing()) {
                            killRandomNoticeReward = killRandomNoticeReward + ";" + PropertyHelper.parsePropertyToString(noticeReward);
                        }
                        rewardBuilder.append(eHuaShiGangMessageType.BOSS_MONSTER_KILL.getValue());
                        rewardBuilder.append(GameConfig.TALK_SEPARATOR);
                        rewardBuilder.append(talentId);//派遣的门客
                        rewardBuilder.append(GameConfig.TALK_SEPARATOR);
                        rewardBuilder.append(damage);
                        rewardBuilder.append(GameConfig.TALK_SEPARATOR);
                        rewardBuilder.append(config.getEventId());
                        rewardBuilder.append(GameConfig.TALK_SEPARATOR);
                        rewardBuilder.append(killRandomNoticeReward);
                    }

                    WorldMessageProto.WorldMessageTempMsg.Builder msg = WorldMessagePb.parseWorldMessageTempMsg(player.getUserInfo(), eMessageType.HuaShiGangSystemMsg.getValue(), eChatContentType.Common.getContentType(), rewardBuilder.toString(), System.currentTimeMillis(), activityId, null);
                    WorldMessageMgr.sendCrossActivityChat(msg);

                }
            }
            //珍兽绑定 鱼类绑定
            player.getModule(VehicleModule.class).bindPatrons(talentId, activityId);

            // 积分铲子奖励
            List<Integer> digItemParam = HuaShiGangMgr.getNewConfig().getAttackDigItemGetParam();
            Property property = new Property();
            property.addProperty(HuaShiGangMgr.getNewConfig().getDigItemId(), BigInteger.valueOf(costNum * digItemParam.get(1) + digItemParam.get(0)));
            property.addProperty(HuaShiGangMgr.getHuaShiGangScoreId(), BigInteger.valueOf(addScore));

            currencyModule.addCurrency(property, eLogMoneyType.HuaShiGang, eLogMoneyType.HuaShiGangAttackMonster);
            reward = PropertyHelper.parsePropertyToString(property);

//            player.notifyListener(eGamePlayerEventType.HuaShiGangScoreRank.getValue(), addScore);
            player.notifyListener(eGamePlayerEventType.HuaShiGangScoreRank.getValue(), allScore);
            SeasonTaskConditionData conditionData = new SeasonTaskConditionData(BigInteger.valueOf((long)HuaShiGangMgr.getActivityInfo().getType()),BigInteger.valueOf(1L));
            player.notifyListener(eGamePlayerEventType.HuaShiGangSendPatronTask.getValue(), conditionData);
            AutoLogMgr.add(new LogHuaShiGangAttackLog(activityId, HuaShiGangMgr.getChildType(), player.getUserId(), talentId, damage, isKill, eventId, monsterId, reward + ";" + PropertyHelper.parsePropertyToString(killProperty)));
        }


        HuaShiGangProto.AttackMonsterRespMsg.Builder builder = HuaShiGangProto.AttackMonsterRespMsg.newBuilder();
        builder.setRet(ret);
        builder.setDamage(damage);
        builder.setNewBlood(newBlood);
        builder.setScore(addScore);
        if (builder.getNewBlood() <= 0) {
            builder.setPlayerInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
        }
        builder.setKillReward(PropertyHelper.parsePropertyToString(killProperty));
        builder.setReward(reward);
        player.sendPacket(ClientProtocol.U_HUA_SHI_GANG_ATTACK_MONSTER, builder);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
